package com.hongtech.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.hongtech.common.bean.AjaxResultVo;
import com.hongtech.common.bean.Token;
import com.hongtech.framework.anno.OperateLog;
import com.hongtech.framework.anno.Permission;
import com.hongtech.modules.common.BaseController;
import com.hongtech.modules.system.dto.SysRoleDTO;
import com.hongtech.modules.system.dto.SysUserDTO;
import com.hongtech.modules.system.dto.SysUserRoleDTO;
import com.hongtech.modules.system.entity.SysUserEntity;
import com.hongtech.modules.system.entity.SysUserRoleEntity;
import com.hongtech.modules.system.service.SysDeptService;
import com.hongtech.modules.system.service.SysRoleService;
import com.hongtech.modules.system.service.SysUserRoleService;
import com.hongtech.modules.system.service.SysUserService;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户控制器
 */
@Controller
@RequestMapping("/system/sysuser")
public class UserController extends BaseController {

	@Autowired
	private SysUserService sysUserService;

	@Autowired
	private SysRoleService sysRoleService;

	@Autowired
	private SysUserRoleService sysUserRoleService;

	@Autowired
	private SysDeptService sysDeptService;

	/**
	 * 跳转用户页
	 */
	@Permission("system:sysuser:list")
	@RequestMapping("/list")
	public String list() {
		return "system/user/list";
	}

	/**
	 * 分页查询用户
	 */
	@Permission("system:sysuser:list")
	@RequestMapping("/page")
	@ResponseBody
	public AjaxResultVo page(String search, Integer offset, Integer limit) {
		PageHelper.startPage(getPageNo(limit, offset), limit);
		List<Map<Object, Object>> userList = sysUserService.listUserPage(search);
		PageInfo<Map<Object, Object>> pageData = new PageInfo<>(userList);
		AjaxResultVo resultVo = AjaxResultVo.success();
		resultVo.put("total", pageData.getTotal());
		resultVo.put("rows", pageData.getList());
		return resultVo;
	}

	/**
	 * 新增用户
	 */
	@Permission("system:sysuser:save")
	@RequestMapping("/add")
	public String add(Model model) {
		model.addAttribute("roleList", sysRoleService.list());
		model.addAttribute("deptList", sysDeptService.list());
		return "system/user/add";
	}

	/**
	 * 执行新增
	 */
	@OperateLog("创建用户")
	@Permission("system:sysuser:save")
	@RequestMapping("/doAdd")
	@ResponseBody
	public AjaxResultVo doAdd(SysUserEntity user, Long[] roleId) {
		sysUserService.insertUser(user, roleId);
		return AjaxResultVo.success();
	}

	/**
	 * 编辑用户
	 */
	@Permission("system:sysuser:edit")
	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable Long id, Model model) {
		SysUserDTO sysUser = sysUserService.get(id);
		List<SysRoleDTO> sysRoles = sysRoleService.list(null);
		LambdaQueryWrapper<SysUserRoleEntity> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysUserRoleEntity::getUserId, id);
		List<SysUserRoleDTO> mySysUserRoles = sysUserRoleService.list(queryWrapper);
		List<String> myRoles = Lists.newArrayList();
		for (SysUserRoleDTO userRole: mySysUserRoles) {
			myRoles.add(String.valueOf(userRole.getRoleId()));
		}
		model.addAttribute("sysUser", sysUser);
		model.addAttribute("sysRoles", sysRoles);
		model.addAttribute("myRoles", myRoles);
		model.addAttribute("deptList", sysDeptService.list());
		return "system/user/edit";
	}

	/**
	 * 执行编辑
	 */
	@OperateLog("编辑用户")
	@Permission("system:sysuser:edit")
	@RequestMapping("/doEdit")
	@ResponseBody
	public AjaxResultVo doEdit(SysUserEntity sysUserEntity, Long[] roleId) {
		sysUserService.updateUser(sysUserEntity, roleId);
		return AjaxResultVo.success();
	}

	/**
	 * 验证用户名是否已存在
	 */
	@RequestMapping("/checkName")
	@ResponseBody
	public AjaxResultVo checkName(String userName) {
		LambdaQueryWrapper<SysUserEntity> ew = new LambdaQueryWrapper<>();
		ew.eq(SysUserEntity::getUserName, userName);
		List<SysUserDTO> list = sysUserService.list(ew);
		if (list.size() > 0) {
			return AjaxResultVo.error(userName + " 用户名已存在,请换一个尝试.");
		}
		return AjaxResultVo.success();
	}

	/**
	 * 删除用户
	 */
	@OperateLog("删除用户")
	@Permission("system:sysuser:remove")
	@RequestMapping("/delete")
	@ResponseBody
	public AjaxResultVo delete(HttpServletRequest request, Long id) {
		Token token = getSSOToken(request);
		if (id.equals(token.getUid())) {
			return AjaxResultVo.error("当前用户不能删除");
		}
		sysUserService.delete(Lists.newArrayList(id));
		return AjaxResultVo.success();
	}

}
